<?php

namespace app\common\model;

use lib\Singleton;
use think\Model;

class Mod extends Model
{

    /**
     * @var bool
     */
    protected $autoWriteTimestamp = true;
    const RETURN_VALUE = 0;

    /**
     * @param array $data
     */
    public function __construct(array $data = [])
    {
        parent::__construct($data);
    }

    /**
     * 通过条件查询分页的数据
     * @param array $map
     * @param int $limits
     * @param $order
     * @param array $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(array $map, int $limits = 10, $order = 'id desc', array $param = [])
    {
        return $this->where($map)->order($order)->paginate($limits, false, ['query' => $param]);
    }

    /**
     * 通过条件查询数据
     * @param array $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function selectTable(array $where = [])
    {
        if (count($where) < 1) {
            return $this->select()->toArray();
        }
        return $this->where($where)->select()->toArray();
    }

    /**
     *通过条件获取分页数据
     * @param array $map
     * @param array $param
     * @param int $limits
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getTablePageList(array $map, array $param = [], int $limits = 10): array
    {
        if (count($map) < 1) {
            return $this->paginate($limits, false, ['query' => $param])->toArray();
        }
        return $this->where($map)->paginate($limits, false, ['query' => $param])->toArray();
    }

    /**
     * 查询单条数据
     * @param int $id
     * @param $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOne(int $id, $field='*'): array
    {
        if (empty($id) || !is_numeric($id)) {
            return [];
        }
        return $this->field($field)->find($id)->toArray();
    }

    /**
     * 通过条件查询总记录
     * @param array $where
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function countTable(array $where = []): int
    {
        if (count($where) < 1) {
            return $this->count();
        }
        return $this->where($where)->count();
    }

    /**
     * 插入数据
     * @param array $data
     * @return int
     */
    public function insertTable(array $data = []): int
    {
        if (!is_array($data) || count($data) < 1) {
            return self::RETURN_VALUE;
        }
        return $this->insertGetId($data);
    }

    /**
     * 更新数据
     * @param $where
     * @param $data
     * @return Common|int
     */
    public function updateTable($where = [], $data = [])
    {
        if (!is_array($data) || count($data) < 1 || count($where) < 1) {
            return self::RETURN_VALUE;
        }
        return $this->where($where)->update($data);
    }

    /**
     *通过条件查询自定义数据
     * @param array $where
     * @param $field
     * @param $order
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function selectTableFields(array $where = [], $field = "*", $order = "id desc"): array
    {
        if (count($where) < 1) {
            return $this->field($field)->select()->toArray();
        }
        return $this->field($field)->where($where)->order($order)->select()->toArray();
    }

    /**
     * 批量插入数据
     * @param array $param
     * @return int
     */
    public function insertManyData(array $param = []): int
    {
        if (is_array($param) && count($param) > 0) {
            return $this->insertAll($param);
        }
        return self::RETURN_VALUE;
    }

    /**
     *通过条件获取单条记录
     * @param array $where
     * @return Common|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOneRecord(array $where)
    {
        if (!is_array($where) || count($where) < 1) {
            return [];
        }
        return $this->where($where)->find();
    }

    /**
     *通过条件查询数据且分页
     * @param array $map
     * @param $field
     * @param int $page
     * @param $limits
     * @param $order
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function selectTableDataByPageWhere(array $map,$field = "*", int $page, $limits = 10, $order = 'id desc')
    {
        return $this->where($map)->page($page, $limits)->field($field)->order($order)->select()->toArray();
    }

    /**
     *
     * @auther menghaoyu
     * @param array $where
     * @param $field
     * @param $order
     * @param int $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function selectRandDataByLimit(array $where = [], $field = "*", $order = "id asc",int $limit = 5): array
    {
        if (count($where) < 1) {
            return $this->field($field)->order($order)->limit($limit)->select()->toArray();
        }
        return $this->field($field)->where($where)->order($order)->limit($limit)->select()->toArray();
    }

    /**
     *通过条件查询单条自定义数据
     * @param array $where
     * @param string $field
     * @return array|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOneInfo(array $where, $field = "*")
    {
        if (!is_array($where) || count($where) < 1) {
            return [];
        }
        return $this->field($field)->where($where)->find();
    }

    /**
     *通过条件查询随机单条记录数据
     * @date: 2020/3/15 16:24
     * @param $where
     * @param string $field
     * @param int $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getRandomOneInfo($where, $field = "*", int $limit = 1)
    {
        if (!is_array($where) || count($where) < 1) {
            return [];
        }
        return $this->field($field)->where($where)->order('id', 'rand()')->limit($limit)->find()->toArray();
    }

    /**
     *查询条件排序后的自定义字段数据

     * @date: 2020/3/20 14:21
     * @param array $where
     * @param string $field
     * @param array $order
     * @return array|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOneInfoWhereOrder(array $where, $field = "*", $order = ["id" => "asc"])
    {
        if (!is_array($where) || count($where) < 1) {
            return [];
        }
        return $this->field($field)->where($where)->order($order)->find();
    }

    /**
     *通过条件查询最大的数据
     * @date: 2020/3/21 15:05
     * @param array $where
     * @param string $field
     * @return array|mixed
     */
    public function getMaxValue(array $where, $field = "*")
    {
        if (!is_array($where) || count($where) < 1) {
            return [];
        }
        return $this->where($where)->max($field);
    }

    /**
     *通过条件查询返回对象数据
     * @date: 2020/3/24 19:18
     * @param array $where
     * @param string $field
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function selectObjectTableFields(array $where = [], $field = "*")
    {
        if (count($where) < 1) {
            return $this->field($field)->select();
        }
        return $this->field($field)->where($where)->select();
    }

    /**
     *根据条件删除数据
     * @date: 2020/3/31 17:25
     * @param array $where
     * @return bool
     * @throws \Exception
     */
    public function deleteTables(array $where)
    {
        if (count($where) < 1) {
            return false;
        }
        return $this->where($where)->delete();

    }
    /**
     *通过条件查询最小的数据

     * @date: 2020/3/21 15:05
     * @param array $where
     * @param string $field
     * @return array|mixed
     */
    public function getMinValue(array $where, $field = "*")
    {
        if (!is_array($where) || count($where) < 1) {
            return [];
        }
        return $this->where($where)->min($field);
    }

    /**
     *通过条件获取分类单条数据

     * @date: 2020/4/13 1:52
     * @param array $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOneRecordData(array $where)
    {
        if (!is_array($where) || count($where) < 1) {
            return [];
        }
        return $this->where($where)->find();
    }



    /**
     *分页查询固定字段的数据

     * @date: 2020/5/12 15:44
     * @param array $map
     * @param int $page
     * @param int $limits
     * @param string $fields
     * @param string $order
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function selectTableFieldsListDataByPageWhere(array $map, int $page, $limits = 10, $fields="*",$order = 'id desc')
    {
        return $this->field($fields)->where($map)->page($page, $limits)->order($order)->select()->toArray();
    }

    /**
     *通过条件查询已排序的数据

     * @date: 2020/5/29 10:46
     * @param array $where
     * @param string $field
     * @param string $order
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function selectOrderTableFieldsList(array $where = [], $field = "*",$order = 'id desc'): array
    {
        if (count($where) < 1) {
            return $this->field($field)->order($order)->select()->toArray();
        }
        return $this->field($field)->where($where)->order($order)->select()->toArray();
    }

    /**
     * 获取一条数据
     * @param $id
     * @param $field
     * @return array|null|Model
     * @auther menghaoyu
     * @date 2020/10/20
     */
    public static function getOneById($id,$field='*'){
        if(!$id) return [];
        return self::field($field)->where('id',$id)->find();
    }

    /**
     * 查询条件获取单条数据信息
     * @param array $where  查询条件
     * @param string $filed  查询字段
     * @return array|null|Model  返回查询结果
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getTableInfoByWhere(array $where , $filed = "*"){
        if (!is_array($where) || !isset($where)) return null;
        return self::field($filed)->where($where)->find();
    }

    /**
     *
     * 倒序查询一条数据
     * @param $channel
     * @param $filed
     * @param $order
     * @return Mod|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDesc($channel = "",$filed = "*" ,$order = 'id desc'){
        if ($channel){
            return $this->where(['channel'=>$channel])->field($filed)->order($order)->find();
        }
        return $this->field($filed)->order($order)->find();
    }

}
